#include <iostream>
#include <fstream>
#include <cstdio>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include "Library_view_achieve.h"
#include"Manager_view_achieve.h"
#include "Borrow_view_achieve.h"
#include "Tool.h"

using namespace std;
using namespace LB;

void main_login(void)
{
	while(1)
	{
		int choice = 0;
		do
		{
			system("clear");			 
			puts("	   ╭════════════════════════════════════════════════════╮");
   			puts("	   │              欢迎光临 Number one 图书馆　　　　    │");  
		    puts("	   ╰════■□■□══════════════════════════════════■□■□══════╯"); 
		    puts("");
		    puts("			   【1】 超管登录");
			puts("			   【2】 用户登录");				
			puts("			   【3】 管理员登录");
			puts("			   【4】 退出系统");
			puts("");
			puts("	   ╭════════════════════════════════════════════════════╮"); 
			puts("	   ╰════■□■□══════════════════════════════════■□■□══════╯");
			puts("");
			cout << "				 请选择: ";
			cin >> choice;
		}while( choice <1 || choice >4);
		system("clear");
		switch(choice)
		{
			case 1:
				{
					Manager_view* M = new Manager_view_achieve ;
					M->menu();	delete M;	break;
				}
			case 2:
				{
					Borrow_view* B = new Borrow_view_achieve;  
					B->menu();	delete B;	break;
				}
			case 3:
				{
					Manager_view* N = new Manager_view_achieve;
					N->login();	delete N; break;
				}
			case 4: 
			{
				quit(); return ;
			}
			
		}
		clear_stdin();		
	}	
}

void anykey2c(void)
{
	cout << "Enter anykey to continue..." ;
	getch();
}

//清理输入缓冲区
void clear_stdin(void)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
}

int getch(void)
{
	//get terminal profile(backup)
	struct termios old;
	int ret = tcgetattr(STDIN_FILENO, &old);
	if (ret < 0)
	{
		perror("tcgetattr");
		return -1;
	}

	//new terminal profile and cancel echo and ensure
	struct termios _new = old;
	_new.c_lflag &= ~(ICANON|ECHO);

	//set the terminal profile
	ret = tcsetattr(STDIN_FILENO, TCSANOW, &_new);
	if (ret < 0)
	{
		perror("tcgsetattr");
		return -2;
	}

	//get info through new profile
	int key_value = 0;
	do
	{
		key_value += getchar();
	} while (stdin->_IO_read_end - stdin->_IO_read_ptr);

	//reset terminal profile
	ret = tcsetattr(STDIN_FILENO, TCSANOW, &old);
	if (ret < 0)
	{
		perror("tcsetattr");
		return -3;
	}

	//return the key value
	return key_value;
}

// 初始化id文件
int init_id(const char* path,int id)
{
	//测试是否存在 F_OK 
	if(0 == access(path,F_OK))
	{
		return 0;
	}
	//如果不存在 则创建
	int fd = open(path,O_WRONLY | O_CREAT | O_TRUNC,0644);
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}
	//"account/id.dat" 写入201808000
	if(0 >= write(fd,&id,sizeof(id)))
	{
		perror("error");
		return -1;
	}

	close(fd);
	return 0;
}


int getid(const char* path)
{
	int id = 0;
	
	int fd = open(path,O_RDWR);
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}

	if(0 >= read(fd,&id,sizeof(id)))
	{
		perror("read");
		return -1;
	}

	id++;	

	lseek(fd,SEEK_SET,0);

	if(0 >= write(fd,&id,sizeof(id)))
	{
		perror("write");
		return -1;
	}

	close(fd);
	
	return id - 1;

}

void loading(void)
{
	struct winsize size;
    ioctl(STDIN_FILENO,TIOCGWINSZ,&size);

    progress_t bar;   
    progress_init(&bar, "", size.ws_col-7, PROGRESS_CHR_STYLE);
    
    int i;
    for ( i = 0; i <= size.ws_col-7; i++ ) 
	{
    	progress_show(&bar, i/((size.ws_col-7)*1.0f));
    	usleep(13000);
    }
    printf("\n");
    progress_destroy(&bar);

}

void login_loading(void)
{
	system("clear");
	for(int i = 0; i < 10 ; i++)
	{
		puts("");
	}
	progress_t bar;
	char buff[255]= {};

	struct winsize size;
	ioctl(STDIN_FILENO,TIOCGWINSZ,&size);
	
	progress_init(&bar, buff,size.ws_col-6, PROGRESS_BGC_STYLE);
	int i;
	for ( i = 0; i <= size.ws_col-6; i++ ) 
	{
		progress_show(&bar, i/((size.ws_col-6)*1.0f));
		usleep(20000);
	}
	progress_destroy(&bar);
}

extern void progress_init(progress_t *bar, const char *title, int max, int style)
{
    bar->chr = '=';
    bar->title = title;
    bar->style = style;
    bar->max = max;
    bar->offset = 100 / (float)max;
    bar->pro = (char *) malloc(max+1);
    if ( style == PROGRESS_BGC_STYLE )
    memset(bar->pro, 0x00, max+1);
    else {
    memset(bar->pro, 32, max);
    memset(bar->pro+max, 0x00, 1);
    }
}

extern void progress_show( progress_t *bar, float bit )
{
    int val = (int)(bit * bar->max);
    switch ( bar->style ) 
    {
	    case PROGRESS_NUM_STYLE:
		    printf("\033[?25l\033[31m\033[1m%s%d%%\033[?25h\033[0m\r",
		        bar->title, (int)(bar->offset * val));
		    fflush(stdout);
		    break;
	    case PROGRESS_CHR_STYLE:
		    memset(bar->pro, '=', val);
		    printf("\033[?25l\033[31m\033[1m%s[%-s] %d%%\033[?25h\033[0m\r", 
		        bar->title, bar->pro, (int)(bar->offset * val));
		    fflush(stdout);
		    break;
	    case PROGRESS_BGC_STYLE:
		    memset(bar->pro, 32, val);
		    printf("\033[?25l\033[31m\033[1m%s\033[41m %d%% %s\033[?25h\033[0m\r", 
		        bar->title, (int)(bar->offset * val), bar->pro);
		    fflush(stdout);
	    break;
    }
}

//destroy the the progress bar.
extern void progress_destroy(progress_t *bar)
{
    free(bar->pro);
}

int get_days()
{
	time_t t=time(NULL);
	struct tm* tt=localtime(&t);
	//struct tm info={0};
	//info.tm_year=tt->tm_year;
	//info.tm_mon=tt->tm_mon;
	//info.tm_mday=tt->tm_mday;
	int seconds=(int)mktime(tt);
	return seconds%10000;

}
//打印等级
void print_star(float praise)
{
	int n = (int)praise ;
	printf("\33[1;31m");//设置字体是蓝色
	for(int i = 0; i < n ;i++)
	{
		printf("★");
	}
	printf("\33[0m\n");//字体颜色设置结束
}

void quit(void)
{
	printf("\33[1;34m");//设置字体是蓝色
	puts("		  Number one 图书馆 开发人员:");
	puts("");
	puts("			   1、林豪");
	puts("");
	puts("			   2、李政");
	puts("");
	puts("			   3、杨超");
	puts("");
	puts("			   4、王曦文");
	puts("");
	printf("\033[?25l");// 隐藏光标
	printf("\33[0m\n");
	sleep(3);
	system("clear");
	
	for(int i = 0; i <9; i++)
	{
		puts("");
	}
	printf("\33[1;33m");//设置字体是黄色
	puts("		   　欢迎下次光临 Number one 图书馆 !");
	clear_stdin();
	sleep(3);
	printf("\33[0m\n");//字体颜色设置结束
	system("clear");
	printf("\033[?25h"); //显示光标
}
